(function($) {

		$.fn.getSelection = function() {
			var e = $(this).get(0);
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					var l = e.selectionEnd - e.selectionStart;
					return { start: e.selectionStart, end: e.selectionEnd, length: l, text: $(e).val().substr(e.selectionStart, l) };
				}) ||
				/* exploder */
				(document.selection && function() {
					e.focus();
					var r = document.selection.createRange();
					if (r == null) {
						return { start: 0, end: $(e).val().length, length: 0 }
					}
					var re = e.createTextRange();
					var rc = re.duplicate();
					re.moveToBookmark(r.getBookmark());
					rc.setEndPoint('EndToStart', re);

					return { start: rc.text.length, end: rc.text.length + r.text.length, length: r.text.length, text: r.text };
				}) ||
				/* browser not supported */
				function() {
					return { start: 0, end: $(e).val().length, length: 0 };
				}
			)();
		}

		$.fn.replaceSelection = function() {
			var e = $(this).get(0);
			var text = arguments[0] || '';
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
                    var value = $(e).val();
                    var pos = e.selectionStart+value.length;
					$(e).val(value.substr(0, e.selectionStart) + text + value.substr(e.selectionEnd, value.length));
                    e.setSelectionRange(pos, pos);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
					document.selection.createRange().text = text;
					return this;
				}) ||

				/* browser not supported */
				function() {
					$(e).val(text);
					return this;
				}
			)();
		};

		$.fn.moveCursor = function() {
			var e = $(this).get(0);
			var pos = arguments[0] || 0;
			return (
				/* mozilla / dom 3.0 */
				('selectionStart' in e && function() {
					e.setSelectionRange(pos, pos);
					return this;
				}) ||

				/* exploder */
				(document.selection && function() {
					e.focus();
                    var range = e.createTextRange();
                    range.move('character', pos);
                    range.select();
					return this;
				}) ||

				/* browser not supported */
				function() {
					e.focus();
					return this;
				}
			)();
		};
})(jQuery);
